package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.*;
import com.gec.model.vo.ITripHotelRoomVo;
import com.gec.model.vo.ITripHotelVo;
import com.gec.system.common.Result;
import com.gec.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 酒店房间表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/system/itrHotelRoom")
@Api(tags="酒店房间管理")
@CrossOrigin
public class ITripHotelRoomController {
    @Autowired
    private ITripHotelRoomService iTripHotelRoomService;

    @Autowired
    private ITripHotelService iTripHotelService;

    @Autowired
    private ITripRoomTypeService iTripRoomTypeService;

    @Autowired
    private ITripHotelOrderService iTripHotelOrderService;

    @Autowired
    private ITripImageService iTripImageService;


//    用户选择酒店，需要传酒店订单信息过来
    @ApiOperation("用户选择酒店房间")
    @PostMapping("/chooseHotelAndRoom")
    public Result chooseHotelAndRoom(
            @RequestBody  ITripHotelOrder ith
    ) {
        BigDecimal price = null;

            Long roomId = ith.getRoomId();

            //      通过id查询房间所有信息
            ITripHotelRoom hr = iTripHotelRoomService.getById(roomId);
            //          获取该房间的数量
            Integer store = hr.getStore();
            //判断当前票数
            boolean isTrue = ith.getNumber() >= store;
            if (!isTrue) {

                return Result.fail("当前票数不足");
            }
            //减去用户买的房间的数量
            hr.setNumber( store - ith.getNumber());
            iTripHotelRoomService.saveOrUpdate(hr);
            //        票数量*票价
            price = price.add(hr.getPrice().multiply(BigDecimal.valueOf(ith.getNumber())));


        ith.setPrice(price);
        iTripHotelOrderService.updateById(ith);
        return Result.ok();
    }


    @ApiOperation("uni-app 获取全部酒店房间")
    @GetMapping("/findAll")
    public Result findAll(){
        List<ITripHotelRoom> list = this.iTripHotelRoomService.selectPage();
        return Result.ok(list);
    }

    @ApiOperation("根据id去移除一个角色")
    // 测试删除
    @DeleteMapping("/removeHotelRoom/{id}")
    public Result removeHotelRoom(@PathVariable Long id)
    {
        boolean b = this.iTripHotelRoomService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findHotelByPageQuery(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       ITripHotelRoomVo iTripHotelRoomVo)
    {
        //1.创建分页对象
        IPage<ITripHotelRoom> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.iTripHotelRoomService.selectPage(p1,iTripHotelRoomVo);
        //3.返回
        return Result.ok(p1);
    }

//    添加房间前选择酒店
    @GetMapping("findAllHotel")
    public Result findAllHotel(){
        List<ITripHotel> list = iTripHotelService.list();
        return Result.ok(list);
    }

    //    添加房间前选择房间类型
    @GetMapping("findAllRoomType")
    public Result findAllRoomType(){
        List<ITripRoomType> list = iTripRoomTypeService.list();
        return Result.ok(list);
    }

    // 添加

    @PostMapping("/addHotelRoom")
    public Result addHotelRoom(
            @RequestBody ITripHotelRoom iTripHotelRoom)
    {
        ITripImage image = new ITripImage();
        image.setImage(iTripHotelRoom.getImage());
        image.setType("2");
        image.setUrl(iTripHotelRoom.getUrl());
        image.setTargetId(iTripHotelRoom.getId());
        iTripImageService.save(image);
        boolean res = this.iTripHotelRoomService.save(iTripHotelRoom);
        if (res)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 修改

    @GetMapping("/findHotelRoomById/{id}")
    public Result findHotelRoomById(@PathVariable Long id)
    {
        ITripHotelRoom iTripHotelRoom = this.iTripHotelRoomService.getById(id);

        return Result.ok(iTripHotelRoom);
    }



    // 实现修改
    @PostMapping("/updateHotelRoom")
    public Result updateHotelRoom(@RequestBody ITripHotelRoom iTripHotelRoom)
    {
        boolean b = this.iTripHotelRoomService.updateById(iTripHotelRoom);
        ITripImage image = new ITripImage();
        image.setImage(iTripHotelRoom.getImage());
        image.setType("1");
        image.setUrl(iTripHotelRoom.getUrl());
        image.setTargetId(iTripHotelRoom.getId());
        Long id = iTripImageService.getId(1,iTripHotelRoom.getId());
        image.setId(id);
        iTripImageService.saveOrUpdate(image);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @DeleteMapping("/removeHotelByIds")
    public Result removeHotelByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.iTripHotelRoomService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}

